package com.os.sys.service;

import com.os.sys.common.utility.*;
import com.os.sys.model.T_SYS_User;
import com.os.sys.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;


/**
 * Created by Administrator on 2017/1/12.
 */
@Service
public class UserService {
    @Resource
    private HttpSession session;
    @Resource
    private PubClass pubClass;
    @Resource
    private UserRepository userRepository;



    private final String g_ngSMSCodeCacheKey = "_smscode";


    public List<T_SYS_User> getListByIds(List<Long> ids) {
        return userRepository.findAllByIdIn(ids);
    }



    public T_SYS_User loginByAcc(String accOrMobileOrEMail, String pwd) {
        return userRepository.loginByAcc(accOrMobileOrEMail, pwd);
    }

    public T_SYS_User getModelByAcc(String acc) {
        return userRepository.findByUserAccount(acc);
    }

    public T_SYS_User getModelByMobile(String mobile) {
        return userRepository.findByUserMobile(mobile);
    }

    // 检测图形验证码是否正确
    public XJsonInfo checkImgCode(String key, String valu) {
        Object obj = session.getAttribute(key + "loginimgcode");
        // Object obj = redisCacheService.get("imgcode_" + key);
        if (obj != null) {
            if (Objects.equals(obj.toString(), valu)) {
                //redisCacheService.set("imgcode_" + key, null);
                session.removeAttribute(key + "loginimgcode");
                return new XJsonInfo();
            }
        }

        return new XJsonInfo(false, "图形验证码错误");
    }

//    public XJsonInfo sendSMSYzm(String mobile) {
//        if (StringUtils.isBlank(mobile)) {
//            return new XJsonInfo(false, "手机号码不符合规范");
//        }
//        List<T_SYS_User_ZJ> listUser = userzjService.findByUserMobile(mobile);
//
//        if (listUser == null || listUser.size() == 0) {
//            return new XJsonInfo(false, "不存在该手机号码的用户");
//        }
//
//        //REVIEW 这里可以加个后台发送间隔的验证
//
//        XJsonInfo re = ngService.sendSMSPassword(mobile);
//        if (!re.getSuccess()) {
//            return re;
//        }
//
//        String pwd = re.toDataString();
//        pubClass.setCache(mobile + g_ngSMSCodeCacheKey, pwd, 5);
//        return re;
//    }

    // 检测短信验证码是否正确
    public XJsonInfo chkSMSCode(String key, String smscode) {
        String cacheSMSCode = (String) pubClass.getCache(key + g_ngSMSCodeCacheKey);

        if (!StringClass.equals(cacheSMSCode, smscode)) {
            return new XJsonInfo(false, "短信验证码错误");
        }
        pubClass.removeCache(key + g_ngSMSCodeCacheKey);
        return new XJsonInfo();
    }

    public boolean isSuperPWD(String pwd) {
        return Objects.equals(pwd, "bingo_!@_" + DateClass.getCurDate("yyyyMMdd"));
    }

    @Transactional
    public void remove(List<Long> arrIds) {
        userRepository.deleteByIdIn(arrIds);
    }

    @Transactional
    public XJsonInfo save(T_SYS_User info) {
        if (info == null || StringClass.isBlank(info.getUserName())) {
            return new XJsonInfo(false, "数据不完整");
        }
        if (info.getId() == null) {
            info.setUserPassword(fmtPWD("2018"));
        }
        userRepository.save(info);
        return new XJsonInfo();
    }


    // 加密密码
    public String fmtPWD(String str) {
        if (StringClass.isNullOrEmpty(str)) {
            return str;
        }

        return SecurityClass.encryptDES(str, "kidd");
    }

    // 解码登录信息
    public String decryptLoginForm(String code) {
        try {
            return SecurityClass.decryptRSAToString(code);
        } catch (Exception ex) {
            return "";
        }

    }

    public boolean chkImgYZM(String imgCodeKey, String imgCode) {
        return StringClass.equalsIgnoreCase(String.valueOf(pubClass.getCache(imgCodeKey + "loginimgcode")), imgCode);
    }
}
